<?php
namespace app\modules\api\models;

use yii;
use yii\db\Query;

class Bank_bill
{
    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix.'bank_bill';
    }

    /**
     * 银行列表
     * @param int $p
     * @param int $s
     * @param array $where
     * @return array
     */
    public function list($p, $s, $where):array
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if ($where['bank']) {
            $db->andWhere(['bank'=>$where['bank']]);
        }
        if ($where['service_name']) {
            $db->andWhere(['service_name'=>$where['service_name']]);
        }
        if ($where['invocie_num']) {
            $db->andWhere(['invocie_num'=>$where['invocie_num']]);
        }
        if ($where['type']) {
            $db->andWhere(['type'=>$where['type']]);
        }
        if ($where['begin_time']) {
            $db->andWhere(['>=','date',$where['begin_time']]);
        }
        if ($where['end_time']) {
            $db->andWhere(['<=','date',$where['end_time']]);
        }
        if ($where['customer_name']) {
            $db->andWhere(['like','customer_name','customer_name']);
        }
        return $db->orderBy(['date'=>SORT_DESC,'id'=>SORT_DESC])->offset(($p-1)*$s)->limit($s)->all();
    }

    /**银行列表数据size
     * @param array $where 查询条件
     * @return int size
     */
    public function size($where = []):int
    {
        $db = new Query();
        $db->select('count(*) as num')->from($this->table);
        if ($where['bank']) {
            $db->andWhere(['bank'=>$where['bank']]);
        }
        if ($where['service_name']) {
            $db->andWhere(['service_name'=>$where['service_name']]);
        }
        if ($where['invocie_num']) {
            $db->andWhere(['invocie_num'=>$where['invocie_num']]);
        }
        if ($where['type']) {
            $db->andWhere(['type'=>$where['type']]);
        }
        if ($where['begin_time']) {
            $db->andWhere(['>=','date',$where['begin_time']]);
        }
        if ($where['end_time']) {
            $db->andWhere(['<=','date',$where['end_time']]);
        }
        if ($where['customer_name']) {
            $db->andWhere(['like','customer_name','customer_name']);
        }

        $info = $db->one();
        return $info?$info['num']:0;
    }

    /**
     * 累计收入支出计算
     *
     */
    public function caculate($where = [])
    {
        $db = new Query();
        $db->select('sum(in_money) as in_money,sum(out_money) as out_money')->from($this->table);
        if ($where['bank']) {
            $db->andWhere(['bank'=>$where['bank']]);
        }
        if ($where['service_name']) {
            $db->andWhere(['service_name'=>$where['service_name']]);
        }
        if ($where['invocie_num']) {
            $db->andWhere(['invocie_num'=>$where['invocie_num']]);
        }
        if ($where['type']) {
            $db->andWhere(['type'=>$where['type']]);
        }
        if ($where['begin_time']) {
            $db->andWhere(['>=','date',$where['begin_time']]);
        }
        if ($where['end_time']) {
            $db->andWhere(['<=','date',$where['end_time']]);
        }
        if ($where['customer_name']) {
            $db->andWhere(['like','customer_name','customer_name']);
        }

        return $info = $db->one();
    }
}
